﻿using com.IronOne.BoardPACWinAppBO.Agenda;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation.Provider;
using Windows.UI.Xaml.Controls;

namespace com.IronOne.BoardPACWinApp.Extensions
{
    public static class FocusExtension
    {
        public static bool GetIsFocused(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsFocusedProperty);
        }


        public static void SetIsFocused(DependencyObject obj, bool value)
        {
            obj.SetValue(IsFocusedProperty, value);
        }


        public static readonly DependencyProperty IsFocusedProperty =
            DependencyProperty.RegisterAttached(
             "IsFocused", typeof(bool), typeof(FocusExtension),
             new PropertyMetadata(false, OnIsFocusedPropertyChanged));

        static DependencyObject dObject;

        private static void OnIsFocusedPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                var uie = d as Windows.UI.Xaml.Controls.Control;

                if (uie != null)
                {
                    //OnLostFocus(uie, null);
                    dObject = uie;
                    uie.Focus(FocusState.Programmatic);

                }
            }
            else
            {
                var uie = d as Windows.UI.Xaml.Controls.Control;

                if (uie != null)
                {
                    SetIsFocused(uie, false);

                }

                //SetIsFocused(dObject, false);
            }
        }

        private static void OnLostFocus(object sender, RoutedEventArgs e)
        {
            if (sender != null && sender is Control)
            {
                (sender as Windows.UI.Xaml.Controls.Control).SetValue(IsFocusedProperty, false);
            }
        }

    }

    //public static class FocusExtensionFrameWorkElement
    //{
    //    public static bool GetIsFocused(DependencyObject obj)
    //    {
    //        return (bool)obj.GetValue(IsFocusedProperty);
    //    }


    //    public static void SetIsFocused(DependencyObject obj, bool value)
    //    {
    //        obj.SetValue(IsFocusedProperty, value);
    //    }


    //    public static readonly DependencyProperty IsFocusedProperty =
    //        DependencyProperty.RegisterAttached(
    //         "IsFocused", typeof(bool), typeof(FocusExtension),
    //         new PropertyMetadata(false, OnIsFocusedPropertyChanged));

    //    static DependencyObject dObject;

    //    private static void OnIsFocusedPropertyChanged(DependencyObject d,
    //        DependencyPropertyChangedEventArgs e)
    //    {
    //        if ((bool)e.NewValue)
    //        {
    //            var uie = d as Windows.UI.Xaml.FrameworkElement;

    //            if (uie != null)
    //            {
    //                //OnLostFocus(uie, null);
    //                dObject = uie;
    //                uie.foc

    //            }
    //        }
    //        else
    //        {
    //            //var uie = d as Windows.UI.Xaml.Controls.Control;

    //            //if (uie != null)
    //            //{
    //            //    SetIsFocused(uie, false);

    //            //}

    //            SetIsFocused(dObject, false);
    //        }
    //    }

    //    private static void OnLostFocus(object sender, RoutedEventArgs e)
    //    {
    //        if (sender != null && sender is Control)
    //        {
    //            (sender as Windows.UI.Xaml.Controls.Control).SetValue(IsFocusedProperty, false);
    //        }
    //    }

    //}
}
